/**
 * 打印队列状态通知服务
 * 负责管理右下角打印队列状态提示的显示和更新
 */

interface QueueStatus {
    total: number
    pending: number
    printing: number
    completed: number
    failed: number
}

interface TaskInfo {
    id: string
    name: string
    status: 'pending' | 'printing' | 'completed' | 'failed'
}

interface NotificationInstance {
    show: () => void
    hide: () => void
    updateStatus: (status: QueueStatus) => void
    updateCurrentTask: (task: TaskInfo | null) => void
}

class PrintQueueNotificationService {
    private instance: NotificationInstance | null = null
    private updateTimer: NodeJS.Timeout | null = null
    private lastStatus: QueueStatus | null = null

    /**
     * 注册通知组件实例
     */
    registerInstance(instance: NotificationInstance) {
        this.instance = instance
        console.log('打印队列通知服务已注册')
    }

    /**
     * 注销通知组件实例
     */
    unregisterInstance() {
        this.instance = null
        this.clearUpdateTimer()
        console.log('打印队列通知服务已注销')
    }

    /**
     * 显示通知
     */
    show() {
        if (this.instance) {
            this.instance.show()
        }
    }

    /**
     * 隐藏通知
     */
    hide() {
        if (this.instance) {
            this.instance.hide()
        }
    }

    /**
     * 更新队列状态
     */
    updateQueueStatus(status: QueueStatus) {
        if (this.instance) {
            // 检查状态是否有变化
            if (!this.lastStatus || this.hasStatusChanged(this.lastStatus, status)) {
                this.instance.updateStatus(status)
                this.lastStatus = { ...status }

                // 如果队列为空且没有正在进行的任务，延迟隐藏通知
                if (status.total === 0 && status.printing === 0) {
                    this.scheduleHide()
                } else if (status.total > 0) {
                    // 有任务时显示通知
                    this.show()
                }
            }
        }
    }

    /**
     * 更新当前任务信息
     */
    updateCurrentTask(task: TaskInfo | null) {
        if (this.instance) {
            this.instance.updateCurrentTask(task)
        }
    }

    /**
     * 显示打印开始提示
     */
    showPrintStart(taskName: string) {
        this.updateCurrentTask({
            id: Date.now().toString(),
            name: taskName,
            status: 'printing'
        })
    }

    /**
     * 显示打印完成提示
     */
    showPrintComplete(taskName: string) {
        this.updateCurrentTask({
            id: Date.now().toString(),
            name: taskName,
            status: 'completed'
        })

        // 延迟清除当前任务显示
        setTimeout(() => {
            this.updateCurrentTask(null)

            // 检查是否所有任务都完成了，如果是则延迟隐藏通知
            if (this.lastStatus && this.lastStatus.total === this.lastStatus.completed + this.lastStatus.failed) {
                setTimeout(() => {
                    this.hide()
                }, 1000) // 再延迟1秒隐藏
            }
        }, 2000)
    }

    /**
     * 显示打印失败提示
     */
    showPrintFailed(taskName: string) {
        this.updateCurrentTask({
            id: Date.now().toString(),
            name: taskName,
            status: 'failed'
        })

        // 延迟清除当前任务显示
        setTimeout(() => {
            this.updateCurrentTask(null)
        }, 3000)
    }

    /**
     * 检查状态是否有变化
     */
    private hasStatusChanged(oldStatus: QueueStatus, newStatus: QueueStatus): boolean {
        return (
            oldStatus.total !== newStatus.total ||
            oldStatus.pending !== newStatus.pending ||
            oldStatus.printing !== newStatus.printing ||
            oldStatus.completed !== newStatus.completed ||
            oldStatus.failed !== newStatus.failed
        )
    }

    /**
     * 安排延迟隐藏
     */
    private scheduleHide() {
        this.clearUpdateTimer()
        this.updateTimer = setTimeout(() => {
            this.hide()
        }, 3000) // 3秒后隐藏
    }

    /**
     * 清除更新定时器
     */
    private clearUpdateTimer() {
        if (this.updateTimer) {
            clearTimeout(this.updateTimer)
            this.updateTimer = null
        }
    }

    /**
     * 获取服务状态
     */
    getStatus() {
        return {
            hasInstance: !!this.instance,
            lastStatus: this.lastStatus
        }
    }
}

// 创建全局单例
const printQueueNotificationService = new PrintQueueNotificationService()

// 注册到全局对象
if (typeof window !== 'undefined') {
    window.__printQueueNotification__ = printQueueNotificationService
}

// 声明全局类型
declare global {
    interface Window {
        __printQueueNotification__: PrintQueueNotificationService
    }
}

export default printQueueNotificationService
export type { QueueStatus, TaskInfo, NotificationInstance }
